import request, { get, post, put, del } from '../utils/request';
import { BlogPost, UserProfile, Skill, WorkExperience } from '../store';

// API 响应类型定义
export interface ApiResponse<T = any> {
  code: number;
  message: string;
  data: T;
}

export interface PaginationParams {
  page?: number;
  pageSize?: number;
}

export interface PostsParams extends PaginationParams {
  search?: string;
  tag?: string;
  sortBy?: 'date' | 'views' | 'likes';
  order?: 'asc' | 'desc';
}

export interface PostsResponse {
  posts: BlogPost[];
  total: number;
  page: number;
  pageSize: number;
  totalPages: number;
}

// 博客文章相关 API
export const blogApi = {
  // 获取文章列表
  getPosts: (params?: PostsParams): Promise<PostsResponse> => {
    return get('/posts', { params });
  },

  // 获取单篇文章
  getPost: (id: string | number): Promise<BlogPost> => {
    return get(`/posts/${id}`);
  },

  // 获取热门文章
  getFeaturedPosts: (): Promise<BlogPost[]> => {
    return get('/posts/featured');
  },

  // 增加文章浏览量
  incrementViews: (id: string | number): Promise<{ views: number }> => {
    return post(`/posts/${id}/views`);
  },

  // 点赞文章
  likePost: (id: string | number): Promise<{ likes: number }> => {
    return post(`/posts/${id}/like`);
  },

  // 取消点赞
  unlikePost: (id: string | number): Promise<{ likes: number }> => {
    return del(`/posts/${id}/like`);
  },

  // 获取相关文章
  getRelatedPosts: (id: string | number): Promise<BlogPost[]> => {
    return get(`/posts/${id}/related`);
  },

  // 搜索文章
  searchPosts: (keyword: string, params?: PaginationParams): Promise<PostsResponse> => {
    return get('/posts/search', {
      params: { keyword, ...params },
    });
  },
};

// 标签相关 API
export const tagApi = {
  // 获取所有标签
  getTags: (): Promise<string[]> => {
    return get('/tags');
  },

  // 获取标签统计
  getTagStats: (): Promise<Array<{ tag: string; count: number }>> => {
    return get('/tags/stats');
  },

  // 根据标签获取文章
  getPostsByTag: (tag: string, params?: PaginationParams): Promise<PostsResponse> => {
    return get(`/tags/${tag}/posts`, { params });
  },
};

// 用户相关 API
export const userApi = {
  // 获取用户信息
  getProfile: (): Promise<UserProfile> => {
    return get('/user/profile');
  },

  // 更新用户信息
  updateProfile: (data: Partial<UserProfile>): Promise<UserProfile> => {
    return put('/user/profile', data);
  },

  // 获取技能列表
  getSkills: (): Promise<Skill[]> => {
    return get('/user/skills');
  },

  // 获取工作经历
  getWorkExperience: (): Promise<WorkExperience[]> => {
    return get('/user/experience');
  },
};

// 统计相关 API
export const statsApi = {
  // 获取网站统计信息
  getSiteStats: (): Promise<{
    totalPosts: number;
    totalViews: number;
    totalLikes: number;
    totalTags: number;
  }> => {
    return get('/stats/site');
  },

  // 获取热门标签
  getPopularTags: (limit?: number): Promise<Array<{ tag: string; count: number }>> => {
    return get('/stats/tags/popular', { params: { limit } });
  },

  // 获取最近访问统计
  getRecentViews: (days?: number): Promise<Array<{ date: string; views: number }>> => {
    return get('/stats/views/recent', { params: { days } });
  },
};

// 评论相关 API (如果需要的话)
export const commentApi = {
  // 获取文章评论
  getComments: (
    postId: string | number,
    params?: PaginationParams,
  ): Promise<{
    comments: Array<{
      id: number;
      content: string;
      author: string;
      date: string;
      replies?: any[];
    }>;
    total: number;
  }> => {
    return get(`/posts/${postId}/comments`, { params });
  },

  // 添加评论
  addComment: (
    postId: string | number,
    data: {
      content: string;
      author: string;
      email?: string;
    },
  ): Promise<any> => {
    return post(`/posts/${postId}/comments`, data);
  },
};

// 导出所有 API
export const api = {
  blog: blogApi,
  tag: tagApi,
  user: userApi,
  stats: statsApi,
  comment: commentApi,
};

export default api;
